package cn.iocoder.yudao.module.crossborder.risk.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.risk.api.dto.ComplianceCheckDTO;
import cn.iocoder.yudao.module.crossborder.risk.api.dto.FraudDetectionDTO;
import cn.iocoder.yudao.module.crossborder.risk.api.dto.RiskAssessmentDTO;
import cn.iocoder.yudao.module.crossborder.risk.api.dto.RiskAssessmentReqDTO;
import cn.iocoder.yudao.module.crossborder.risk.service.RiskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 风险管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 风险管理")
@RestController
@RequestMapping("/crossborder/risk")
@Validated
public class RiskController {

    @Resource
    private RiskService riskService;

    @PostMapping("/assessment")
    @Operation(summary = "风险评估")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:assessment')")
    public CommonResult<RiskAssessmentDTO> assessRisk(@RequestBody @Valid RiskAssessmentReqDTO assessmentReq) {
        RiskAssessmentDTO result = riskService.assessRisk(assessmentReq);
        return success(result);
    }

    @GetMapping("/assessment/get")
    @Operation(summary = "获得风险评估记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:query')")
    public CommonResult<RiskAssessmentDTO> getRiskAssessment(@RequestParam("id") Long id) {
        RiskAssessmentDTO assessment = riskService.getRiskAssessment(id);
        return success(assessment);
    }

    @PostMapping("/fraud/detect")
    @Operation(summary = "欺诈检测")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1024")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "2048")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:fraud')")
    public CommonResult<FraudDetectionDTO> detectFraud(@RequestParam("userId") Long userId,
                                                       @RequestParam("orderId") Long orderId) {
        FraudDetectionDTO result = riskService.detectFraud(userId, orderId);
        return success(result);
    }

    @PostMapping("/compliance/check")
    @Operation(summary = "合规检查")
    @Parameter(name = "businessType", description = "业务类型", required = true, example = "ORDER")
    @Parameter(name = "businessId", description = "业务编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:compliance')")
    public CommonResult<ComplianceCheckDTO> checkCompliance(@RequestParam("businessType") String businessType,
                                                           @RequestParam("businessId") Long businessId) {
        ComplianceCheckDTO result = riskService.checkCompliance(businessType, businessId);
        return success(result);
    }

    @PostMapping("/blacklist/add")
    @Operation(summary = "添加黑名单")
    @Parameter(name = "type", description = "类型", required = true, example = "USER")
    @Parameter(name = "value", description = "值", required = true, example = "user123")
    @Parameter(name = "reason", description = "原因", required = true, example = "欺诈行为")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:blacklist')")
    public CommonResult<Boolean> addBlacklist(@RequestParam("type") String type,
                                             @RequestParam("value") String value,
                                             @RequestParam("reason") String reason) {
        Boolean result = riskService.addBlacklist(type, value, reason);
        return success(result);
    }

    @PostMapping("/blacklist/remove")
    @Operation(summary = "移除黑名单")
    @Parameter(name = "type", description = "类型", required = true, example = "USER")
    @Parameter(name = "value", description = "值", required = true, example = "user123")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:blacklist')")
    public CommonResult<Boolean> removeBlacklist(@RequestParam("type") String type,
                                                @RequestParam("value") String value) {
        Boolean result = riskService.removeBlacklist(type, value);
        return success(result);
    }

    @GetMapping("/blacklist/check")
    @Operation(summary = "检查黑名单")
    @Parameter(name = "type", description = "类型", required = true, example = "USER")
    @Parameter(name = "value", description = "值", required = true, example = "user123")
    @PreAuthorize("@ss.hasPermission('crossborder:risk:query')")
    public CommonResult<Boolean> checkBlacklist(@RequestParam("type") String type,
                                               @RequestParam("value") String value) {
        Boolean result = riskService.checkBlacklist(type, value);
        return success(result);
    }

}
